const {
    getCommentListServe,
    deleteFeedBackServe,
    getFeedBackByIdServe,
    addFeedBackServe
} = require('../serve/feedBack.serve')
const websocketServer = require('../utils/webSocket')

class FeedBackController {
    async getFeedbackList(ctx) {
        try {
            const res = await getCommentListServe(ctx)
            ctx.body = {
                msg: '获取成功',
                status: 0,
                data: {
                    total: res.total,
                    pageSize: Number(res.pageSize),
                    pageNum: Number(res.pageNum),
                    pageData: res.data
                }
            }
        } catch (e) {
            ctx.body = {
                msg: e.message,
                status: 1
            }
        }
    }

    async deleteFeedBack(ctx) {
        try {
            const {id} = ctx.params
            const res = await getFeedBackByIdServe({id});
            if (!res) {
                console.log('未找到id对应的数据')
                throw ({message: '删除失败'})
            }
            const isSuccessDelete = await deleteFeedBackServe(id);
            ctx.body = {
                msg: '删除成功',
                status: 0,
                data: isSuccessDelete
            }
        } catch (e) {
            ctx.body = {
                msg: e.message,
                status: 1
            }
        }
    }

    async addFeedBack(ctx) {
        try {
            const {content, source} = ctx.request.body
            const {id: createdUserId, nickName: createdUserName} = ctx.state.appletUser
            if (!content) {
                throw({message: '请填写参数'})
            }
            if (content.length <= 100 && content.length > 0) {
                const res = await addFeedBackServe({content, createdUserId, createdUserName,source})
            } else {
                throw({message: 'content参数错误'})
            }

            // if(res){
            //     websocketServer.connections.forEach(connect=>{
            //         connect.send(JSON.stringify(res))
            //     })
            // }
            ctx.body = {
                msg: '添加成功',
                status: 0
            }
        } catch (e) {
            ctx.body = {
                msg: e.message,
                status: 1
            }
        }

    }
}

module.exports = new FeedBackController()
